package com.fortune.conf;

/**
 * @program: fortune
 * @description:
 * @author: wwb
 * @create: 2025-04-16 18:37
 */
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import javax.net.ssl.SSLContext;
import java.security.NoSuchAlgorithmException;

public class CustomHttpClientGenerator {
    public static CloseableHttpClient createHttpClient() throws NoSuchAlgorithmException {
        // 强制使用TLSv1.2协议
        SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslContext,
                new String[]{"TLSv1.2"}, // 允许的协议版本
                null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier()
        );

        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", sslsf)
                .build();

        return HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .setDefaultRequestConfig(RequestConfig.custom()
                        .setSocketTimeout(10000)
                        .setConnectTimeout(10000)
                        .build())
                .build();
    }
}
